<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>

<div id="createDialog" class="container-fluid">
    <div class="row">
        <form id="createForm" class="form-horizontal" method="post">
            <div class="form-group">
                <label class="col-sm-4 control-label" for="jobName">
                    <span class="help-inline"><font color="red">*</font> </span>
                    任务名称：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="jobName" name="jobName" placeholder="任务名称" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="jobGroup" >
                    <span class="help-inline"><font color="red">*</font> </span>
                    任务分组：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="jobGroup" name="jobGroup" placeholder="任务分组" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="cron" >
                    <span class="help-inline"><font color="red">*</font> </span>
                    Cron表达式：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="cron" name="cron" placeholder="Cron表达式" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="isLocal" >
                    <span class="help-inline"><font color="red">*</font> </span>
                    调度方式：</label>
                <div class="col-sm-5">
                    <select id="isLocal" name="isLocal" class="form-control">
                        <option value="1" selected="selected">本地</option>
                        <option value="0">远程</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="isAsync" >
                    <span class="help-inline"><font color="red">*</font> </span>
                    是否异步：</label>
                <div class="col-sm-5">
                    <select id="isAsync" name="isAsync" class="form-control">
                        <option value="1" selected="selected">是</option>
                        <option value="0">否</option>
                    </select>
                </div>
            </div>
            <div class="form-group isRemote" style="display: none;">
                <label class="col-sm-4 control-label" for="remoteUrl" >
                    <span class="help-inline"></span>
                    远程执行url：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="remoteUrl" name="remoteUrl" placeholder="远程执行url" />
                </div>
            </div>
            <div class="form-group isLocal">
                <label class="col-sm-4 control-label" for="beanClass" >
                    <span class="help-inline"></span>
                    执行类名称：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="beanClass" name="beanClass" placeholder="执行类名称" />
                </div>
            </div>
            <div class="form-group isLocal">
                <label class="col-sm-4 control-label" for="methodName" >
                    <span class="help-inline"></span>
                    执行方法：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="methodName" name="methodName" placeholder="执行方法" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="params" >
                    <span class="help-inline"></span>
                    参数：</label>
                <div class="col-sm-5">
                    <textarea class="form-control" id="params" name="params" placeholder="参数"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="remarks" >
                    <span class="help-inline"></span>
                    描述：</label>
                <div class="col-sm-5">
                    <textarea class="form-control" id="remarks" name="remarks" placeholder="描述"></textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-9 col-sm-offset-4">
                    <button type="button" class="btn btn-primary" onclick="createSubmit();">
                        保存
                    </button>
                    <button type="button" class="btn btn-default"
                            onclick="createDialog.close();">取消
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    $(function(){
        $("#isLocal").change(function(e){
            $(".isRemote").css("display",$(this).val() == 1 ? "none":"");
            $(".isLocal").css("display",$(this).val() != 1 ? "none":"");
        });
    });
    function validateForm(){
        return $( "#createForm" ).validate( {
            rules: {
                jobName: {
                    required: true,
                    maxlength: 100
                },
                jobGroup: {
                    required: true,
                    maxlength: 100
                },
                cron: "required",
                isLocal: "required",
                isAsync: "required",
                params: {
                    maxlength: 250
                },
                remarks: {
                    maxlength: 100
                }
            },
            messages: {
                jobName:  {
                    required: "该项为必填项。",
                    maxlength: "长度超出100。"
                },
                jobGroup:  {
                    required: "该项为必填项。",
                    maxlength: "长度超出100。"
                },
                cron: "该项必填项。",
                isLocal: "该项是必填项。",
                isAsync: "该项是必填项。",
                params:  {
                    maxlength: "长度超出250。"
                },
                remarks: {
                    maxlength: "长度超出100。"
                }
            },
            errorElement: "em",
            errorPlacement: function ( error, element ) {
                // Add the `help-block` class to the error element
                error.addClass( "help-block" );

                if ( element.prop( "type" ) === "checkbox" ) {
                    error.insertAfter( element.parent( "label" ) );
                } else {
                    error.insertAfter( element );
                }
            },
            highlight: function ( element, errorClass, validClass ) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
            },
            unhighlight: function (element, errorClass, validClass) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
            }
        } );
    }
    //注册表单验证
    $(validateForm());


    function createSubmit() {
        if(validateForm().form()){
            $.ajax({dataType: 'json',
                type: 'post',
                url: '${basePath}/manage/job/create',
                data: $('#createForm').serialize(),
                success: function(result) {
                    console.log(result);
                    if (result.code != 1) {
                        if (result.data instanceof Array) {
                            $.each(result.data, function(index, value) {
                                infoDialog(value.errorMsg);
                            });
                        } else {
                            infoDialog(result.data);
                        }
                    } else {
                        createDialog.close();
                        $table.bootstrapTable('refresh');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    console.log(textStatus);
                    infoDialog(textStatus);
                }
            });
        }
    }
</script>